package com.pe1n.yangtaopai.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.bean.CollectBean;
import com.pe1n.yangtaopai.helper.IupDataListenerTwo;
import com.pe1n.yangtaopai.page.detail.view.activity.DetailActivity;

import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by My on 2016/11/28.
 */

public class CollectAcyivityAdapter extends RecyclerView.Adapter<CollectAcyivityAdapter.CollectActivityViewHolder> {
    //收集商品的适配器
    private Context mContext;
    private List<CollectBean> mList;
    private AlertDialog alertDialog;//对话框
    private Intent intent;
    private AlertDialog.Builder builder;
    private IupDataListenerTwo iupDataListenerTwo;//回调的接口,处理更新的数据
    public CollectAcyivityAdapter(Context mContext, List<CollectBean> mList,IupDataListenerTwo iupDataListenerTwo) {
        this.mContext = mContext;
        this.mList = mList;
        this.iupDataListenerTwo=iupDataListenerTwo;
        builder=new AlertDialog.Builder(mContext);
    }

    @Override
    public CollectActivityViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         intent=new Intent(mContext, DetailActivity.class);
         View view= LayoutInflater.from(mContext).inflate(R.layout.collectactivity_layout_item,parent,false);
         return new CollectActivityViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CollectActivityViewHolder holder, int position) {
        final CollectBean collectBean=mList.get(position);
        holder.mTextViewName.setText(collectBean.getName());
        String string=collectBean.getPic();
        if (string!=null){
            Glide.with(mContext).load( string)
                    .placeholder(R.drawable.ytjx)
                    .into(holder.imageViewPic);
        }
        else{
            Glide.with(mContext).load(R.drawable.ytjx )
                    .placeholder(R.drawable.ytjx)
                    .into(holder.imageViewPic);
        }
        holder.mTextViewPrice.setText("¥"+collectBean.getPrice());
        holder.mLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("id",collectBean.getId());
                intent.putExtra("name",collectBean.getName());
                mContext.startActivity(intent);
            }
        });
        holder.imageViewDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.setMessage("你确定要删除吗?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                collectBean.delete(new UpdateListener() {
                                    @Override
                                    public void done(BmobException e) {
                                         if( e==null){
                                             Toast.makeText(mContext,"删除收藏成功啦",Toast.LENGTH_SHORT).show();
                                             iupDataListenerTwo.upCollectData();
                                         }
                                        else {
                                             Toast.makeText(mContext,"删除收藏失败啦",Toast.LENGTH_SHORT).show();
                                         }
                                    }
                                });
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                alertDialog.dismiss();
                            }
                        });
                       alertDialog= builder.create();
                alertDialog.show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return  mList.size();
    }

    class  CollectActivityViewHolder extends RecyclerView.ViewHolder{
         TextView mTextViewName;
         ImageView imageViewPic;
         LinearLayout mLinearLayout;
         TextView mTextViewPrice;// 价格
         ImageView imageViewDelete;//删除
        public CollectActivityViewHolder(View itemView) {
             super(itemView);
             imageViewPic= (ImageView) itemView.findViewById(R.id.collect_activity_layout_item_pic);
             mTextViewName= (TextView) itemView.findViewById(R.id.collect_activity_layout_item_name);
             mLinearLayout= (LinearLayout) itemView.findViewById(R.id.collect_activity_layout_item_linearLayout);
             mTextViewPrice= (TextView) itemView.findViewById(R.id.collect_activity_layout_item_price);
             imageViewDelete= (ImageView) itemView.findViewById(R.id.collect_activity_layout_item_delete);
        }
    }
}
